/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main_com.Controlers.friend;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import main_com.mapper.FriendMapper;
import main_com.pojo.LoginBean;
import main_com.pojo.LookFriendBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.IOException;
import java.util.ArrayList;

/**
 * @author Administrator
 */
@Controller
@RequestMapping("/DeleteFriendController")
public class DeleteFriendController {

    //    public void wrong1(){
//        String msg="请输入要删除的人的姓名！";
//        int type=JOptionPane.YES_NO_CANCEL_OPTION;
//        String title="信息提示";
//        JOptionPane.showMessageDialog(null, msg, title, type);
//    }
//    public void wrong2(){
//        String msg="此联系人不存在！";
//        int type=JOptionPane.YES_NO_CANCEL_OPTION;
//        String title="信息提示";
//        JOptionPane.showMessageDialog(null, msg, title, type);
//    }
//    public void right(){
//        String msg="此联系人已成功删除！";
//        int type=JOptionPane.YES_NO_CANCEL_OPTION;
//        String title="信息提示";
//        JOptionPane.showMessageDialog(null, msg, title, type);
//    }
    @Autowired
    FriendMapper friendMapper;

    @RequestMapping("/deleteFriend")
    protected void deleteFriend(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String name = request.getParameter("name");
        String number = request.getParameter("number");
        if (name.isEmpty() || number.isEmpty()) {
//            wrong1();
            response.sendRedirect("http://localhost/friendManager/deleteFriend.jsp");
        } else {
            try {
                String userName = "";
                HttpSession session = request.getSession();
                ArrayList login = (ArrayList) session.getAttribute("login");
                if (login == null || login.size() == 0) {
                    response.sendRedirect("http:///login.jsp");
                } else {
                    for (int i = login.size() - 1; i >= 0; i--) {
                        LoginBean nn = (LoginBean) login.get(i);
                        userName = nn.getUserName();
                    }
                }
                int k = friendMapper.findFriendByNameAndUserNameNumber(name, userName, number);
                if (k < 1) {
//                    wrong2();
                    response.sendRedirect("http://localhost/friendManager/deleteFriend.jsp");
                    return;
                } else {
                    friendMapper.deleteFriendByNameAndUserNameNumber(name, userName, number);
                    // 获取当前页数，默认是第1页
                    String pageParam = request.getParameter("page");
                    int currentPage = (pageParam != null) ? Integer.parseInt(pageParam) : 1;
                    int pageSize = 3; // 每页显示3条数据
                    int offset = (currentPage - 1) * pageSize;

                    // 获取总记录数
                    int totalCount = friendMapper.getTotalFriendCount(userName);
                    int totalPages = (int) Math.ceil((double) totalCount / pageSize);

                    // 将数据和分页信息存入 session
                    session.setAttribute("currentPage", currentPage);
                    session.setAttribute("totalPages", totalPages);
                    ArrayList<LookFriendBean> friendslist = friendMapper.getFriendByUserNameWithPagination(userName, offset, pageSize);
                    session.setAttribute("friendslist", friendslist);
                }
                response.sendRedirect("http://localhost/friendManager/lookFriend.jsp");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


}